package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.service.CartService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class CartController {

    @Autowired
    private CartService cartService;

    // 添加购物车
    @PostMapping("/cart/add")
    public ResultInfo add(@RequestBody Cart cartParam) {
        //调用
        Cart cart = cartService.add(cartParam);
        //返回
        return ResultInfo.success(cart);
    }

    //查询购物车
    @GetMapping("/cart/list")
    public ResultInfo list() {
        //调用+返回
        return ResultInfo.success(cartService.list());
    }

 /*   //修改购物车减少数量
    @PostMapping("/cart/sub")
    public ResultInfo updateByList(@RequestBody Map<String,Long> map){
        //调用
        Cart cart = cartService.updateByList(map);
        //返回
        return ResultInfo.success(cart);
    }*/

    //修改购物车减少数量
    @PostMapping("/cart/sub")
    public ResultInfo sub(@RequestBody Cart cartParam) {
        //调用
        Cart cart = cartService.sub(cartParam);
        //返回
        return ResultInfo.success(cart);
    }

    // 清空购物车
    @DeleteMapping("/cart/clean")
    public ResultInfo clean() {
        // 调用service清理
        cartService.clean();
        // 返回结果
        return ResultInfo.success();
    }
}